#include "at24c64.h"
void at24c64_init(void)
{
	i2c_init();
}
int8_t at24c64_write(uint16_t addr, uint8_t data)
{
	int ret;
	i2c_start();
	i2c_send_byte(AT24C64_WRITE_ADDR);
	ret = i2c_recv_ack();
	if (ret == 1)
	{
		return -1;
	}
	i2c_send_byte(addr >> 8);
	i2c_recv_ack();
	i2c_send_byte(addr & 0xff);
	ret = i2c_recv_ack();
	if (ret == 1)
	{
		return -2;
	}
	i2c_send_byte(data);
	ret = i2c_recv_ack();
	if (ret == 1)
	{
		return -3;
	}
	i2c_stop();
	return AT24C64_OK;
}

int8_t at24c64_read(uint16_t addr, uint8_t *data)
{
	int ret;
	i2c_start();
	i2c_send_byte(AT24C64_WRITE_ADDR);
	ret = i2c_recv_ack();
	if (ret == 1)
	{
		return -1;
	}
	i2c_send_byte(addr >> 8);
	ret = i2c_recv_ack();
	if (ret == 1)
	{
		return -2;
	}
	i2c_send_byte(addr & 0xff);
	ret = i2c_recv_ack();
	if (ret == 1)
	{
		return -3;
	}
	i2c_start();
	i2c_send_byte(AT24C64_READ_ADDR);
	ret = i2c_recv_ack();
	if (ret == 1)
	{
		return -4;
	}
	*data = i2c_recv_byte();
	i2c_send_ack(1);
	i2c_stop();
	return AT24C64_OK;
}
